<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="shortcut icon" href="../../../favicon.ico" type="image/x-icon">
	<title>life_cycle</title>
</head>
<body>
  <div id="test"></div>

  <script type="text/javascript" src="../../../js/react.development.js" ></script>
  <script type="text/javascript" src="../../../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../../../js/babel.min.js"></script>
  <script type="text/javascript" src="../../../js/prop-types.js"></script>

  <script type="text/babel">
    class Count extends React.Component {
      state = {
        count: 0
      }
      constructor(){
        super()
        console.log('constructor');
      }
      
      componentWillMount(){
        console.log('componentWillMount');
      }

      componentWillReceiveProps(){
        console.log('componentWillReceiveProps');
      }

      shouldComponentUpdate(){
        console.log('shouldComponentUpdate')
        return true
      }

      componentWillUpdate(){
        console.log('componentWillUpdate')
      }

      componentDidUpdate(){
        console.log('componentDidUpdate')
      }

      componentDidMount(){
        console.log('componentDidMount')
      }

      componentWillUnmount(){
        console.log('componentWillUnmount')
      }

      add=()=>{
        let {count} = this.state
        count+=1
        this.setState({count})
      }

      unmount=()=>{
        console.log('unmount');
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      forceUpd=()=>{
        console.log('forceUpd');
        this.forceUpdate();
      }

      render(){
        console.log('render')
        const {count} = this.state
        return (
          <div>
            <h2>当前求和为: {count}</h2>
            <button onClick={this.add}>点我+1</button>
            <button onClick={this.unmount}>卸载组件</button>
            <button onClick={this.forceUpd}>不更改数据,强制更新页面</button>
          </div>
        )
      }
    }

    //ReactDOM.render(<Count />, document.getElementById('test'));

    class A extends React.Component{
      state = {carName: '宝骏'}
      change = ()=>{
        this.setState({carName: '比亚迪'})
      }

      constructor(){
        super()
        console.log('A--constructor');
      }

      componentWillMount(){
        console.log('A--componentWillMount');
      }

      componentWillReceiveProps(){
        console.log('A--componentWillReceiveProps');
      }

      shouldComponentUpdate(){
        console.log('A--shouldComponentUpdate')
        return true
      }

      componentWillUpdate(){
        console.log('A--componentWillUpdate')
      }

      componentDidUpdate(){
        console.log('A--componentDidUpdate')
      }

      componentDidMount(){
        console.log('A--componentDidMount')
      }

      componentWillUnmount(){
        console.log('A--componentWillUnmount')
      }

      render(){
        console.log('A--render')
        return (
          <div>
            <h2>A</h2>
            <button onClick={this.change}>换车</button>
            {<B carName={this.state.carName}/>}
          </div>
        )
      }
    }

    class B extends React.Component {
      constructor(props){
        super(props)
        console.log('B--constructor');
      }
      
      componentWillMount(){
        console.log('B--componentWillMount');
      }

      componentWillReceiveProps(){
        console.log('B--componentWillReceiveProps');
      }

      shouldComponentUpdate(){
        console.log('B--shouldComponentUpdate')
        return true
      }

      componentWillUpdate(){
        console.log('B--componentWillUpdate')
      }

      componentDidUpdate(){
        console.log('B--componentDidUpdate')
      }

      componentDidMount(){
        console.log('B--componentDidMount')
      }

      componentWillUnmount(){
        console.log('B--componentWillUnmount')
      }
      render(){
        console.log('B--render')
        const {carName} = this.props

        return (
          <div>
            <h2>B</h2>
            <h2>{carName}</h2>
          </div>
        )
      }
    }

    ReactDOM.render(<A />, document.getElementById('test'));
  </script>
</body>